const { data } = require('../mock')
const bodyParser = require('body-parser')
const Mock = require('mockjs')

module.exports = app => {
    app.use(bodyParser.json())

    app.post('/api/getList', (req, res) => {

        let { type,  examType, className} = req.body

        let newData = data

        if (examType) {
            newData = newData.filter(item => item.examType === examType)
        }

        if (className) {
            newData = newData.filter(item => item.className === className)
        }

        res.send({
            code: 1,
            data: type ? newData.filter(item => item.type === type) : newData
        })
    })


    // 添加
    app.post('/api/addDate', (req, res) => {
        const {
            title,
            examType,
            className,
            date
        } = req.body

        data.unshift({
            title,
            examType,
            className,
            datetimeStart: date[0],
            datatimeEnd: date[1],
            id: Mock.mock("@id"),
            name: Mock.mock("@cname"),
            city: Mock.mock("@city")
        })

        res.send({
            code: 1,
            msg: '添加成功'
        })

    })

}